package com.zhiche.lisa.bms.service.utils.common.identityCodeUtil;

import com.zhiche.lisa.bms.service.increment.IncrementCodeService;
import com.zhiche.lisa.bms.service.utils.common.identityCodeUtil.impl.IncrementServiceCode;
import com.zhiche.lisa.bms.service.utils.common.identityCodeUtil.impl.ServiceCode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @Author: liuanshun
 * @Description: 提供全局的code获取Bean
 * @Date: Create in 15:28 2018/11/3
 */
@Component
public class CodeUtil {
    private final Logger LOGGER = LoggerFactory.getLogger(CodeUtil.class);

    private final String CAPITALARRANGEMENT_CODE_STR = "^PK[yyyy][MM][dd]%[incrementId]";
    private final String PAYAPPLY_CODE_STR = "^P[yyyy][MM][dd]%[incrementId]";
    private final String CONTRACT_CODE_STR = "{CCN}^-FVS-[yyyy]-%[incrementId]";
    private final String INVOICEAPPLY_CODE_STR = "^INV[yyyy][MM][dd]%[incrementId]";
    private final String QUOTE_CODE_STR = "^AT[yyyy][MM][dd]%[incrementId]";
    private final String BILL_CODE_STR = "^AP[yyyy][MM][dd]%[incrementId]";
    private final String PAYAFFIRM_CODE_STR = "^FK[yyyy][MM][dd]%[incrementId]";
    private final String ASSESSMENT_CODE_STR = "^AS[yyyy][MM][dd]%[incrementId]";
    private final String REWARD_CODE_STR = "^JS[yyyy][MM][dd]%[incrementId]";

    @Autowired
    private IncrementCodeService incrementCodeService;

    /**
     * 全局的合同code获取对象
     */
    private static IncrementServiceCode contractCode = null;
    /**
     * 全局的开票申请code获取对象
     */
    private static IncrementServiceCode invoiceApplyCode = null;
    /**
     * 全局的支付申请code获取对象
     */
    private static IncrementServiceCode payApplyCode = null;
    /**
     * 全局的排款计划code获取对象
     */
    private static IncrementServiceCode capitalArrangementCode = null;
    /**
     * 全局的报价单code获取对象
     */
    private static IncrementServiceCode quoteCode = null;
    /**
     * 全局的账单code获取对象
     */
    private static IncrementServiceCode billCode = null;
    /**
     * 全局的付款确认code获取对象
     */
    private static IncrementServiceCode payAffirmCode = null;
    /**
     * 全局的考核单code获取对象
     */
    private static IncrementServiceCode assessmentCode = null;
    /**
     * 全局的考核单code获取对象
     */
    private static IncrementServiceCode rewardCode = null;

    public Boolean removeServiceCode(String codeName) {
        switch (codeName) {
            case "ContractCode":
                CodeUtil.contractCode = null;
                break;
            case "InvoiceApplyCode":
                CodeUtil.invoiceApplyCode = null;
                break;
            case "PayApplyCode":
                CodeUtil.payApplyCode = null;
                break;
            case "CapitalArrangementCode":
                CodeUtil.capitalArrangementCode = null;
                break;
            case "QuoteCode":
                CodeUtil.quoteCode = null;
                break;
            case "BillCode":
                CodeUtil.billCode = null;
                break;
            case "payAffirmCode":
                CodeUtil.payAffirmCode = null;
                break;
            case "assessmentCode":
                CodeUtil.assessmentCode = null;
                break;
            case "rewardCode":
                CodeUtil.rewardCode = null;
                break;
            default:
                return null;
        }
        return true;
    }

    /**
     * 获取code对象，自动初始化
     *
     * @return
     */
    public ServiceCode getContractCode() {
        if (contractCode == null) {
            LOGGER.info("createCodeBind --> Create:ContractCode");
            contractCode = new IncrementServiceCode(incrementCodeService, CONTRACT_CODE_STR, "ContractCode").init();
        }
        return contractCode;
    }

    /**
     * 获取code对象，自动初始化
     *
     * @return
     */
    public ServiceCode getInvoiceApplyCode() {
        if (invoiceApplyCode == null) {
            LOGGER.info("createCodeBind --> Create:InvoiceApplyCode");
            invoiceApplyCode = new IncrementServiceCode(incrementCodeService, INVOICEAPPLY_CODE_STR, "InvoiceApplyCode").init();
        }
        return invoiceApplyCode;
    }

    /**
     * 获取code对象，自动初始化
     *
     * @return
     */
    public ServiceCode getPayApplyCode() {
        if (payApplyCode == null) {
            LOGGER.info("createCodeBind --> Create:PayApplyCode");
            payApplyCode = new IncrementServiceCode(incrementCodeService, PAYAPPLY_CODE_STR, "PayApplyCode").init();
        }
        return payApplyCode;
    }

    /**
     * 获取code对象，自动初始化
     *
     * @return
     */
    public ServiceCode getCapitalArrangementCode() {
        if (capitalArrangementCode == null) {
            LOGGER.info("createCodeBind --> Create:CapitalArrangementCode");
            capitalArrangementCode = new IncrementServiceCode(incrementCodeService, CAPITALARRANGEMENT_CODE_STR, "CapitalArrangementCode").init();
        }
        return capitalArrangementCode;
    }

    /**
     * 获取code对象，自动初始化
     *
     * @return
     */
    public ServiceCode getQuoteCode() {
        if (quoteCode == null) {
            LOGGER.info("createCodeBind --> Create:QuoteCode");
            quoteCode = new IncrementServiceCode(incrementCodeService, QUOTE_CODE_STR, "QuoteCode").init();
        }
        return quoteCode;
    }

    /**
     * 获取code对象，自动初始化
     *
     * @return
     */
    public ServiceCode getBillCode() {
        if (billCode == null) {
            LOGGER.info("createCodeBind --> Create:BillCode");
            billCode = new IncrementServiceCode(incrementCodeService, BILL_CODE_STR, "BillCode").init();
        }
        return billCode;
    }

    /**
     * 获取code对象，自动初始化
     *
     * @return
     */
    public ServiceCode getPayAffirmCode() {
        if (payAffirmCode == null) {
            LOGGER.info("createCodeBind --> Create:payAffirmCode");
            payAffirmCode = new IncrementServiceCode(incrementCodeService, PAYAFFIRM_CODE_STR, "payAffirmCode").init();
        }
        return payAffirmCode;
    }

    /**
     * 获取code对象，自动初始化 - 考核单
     *
     * @return
     */
    public ServiceCode getAssessmentCode() {
        if (assessmentCode == null) {
            LOGGER.info("createCodeBind --> Create:assessmentCode");
            assessmentCode = new IncrementServiceCode(incrementCodeService, ASSESSMENT_CODE_STR, "assessmentCode").init();
        }
        return assessmentCode;
    }

    /**
     * 获取code对象，自动初始化 - 考核单
     *
     * @return
     */
    public ServiceCode getRewartCode() {
        if (rewardCode == null) {
            LOGGER.info("createCodeBind --> Create:rewardCode");
            rewardCode = new IncrementServiceCode(incrementCodeService, REWARD_CODE_STR, "rewardCode").init();
        }
        return rewardCode;
    }
}
